import json

import execjs
import requests

url = 'https://dict.youdao.com/webtranslate/key'  # 获取翻译解密参数
url2 = 'https://dict.youdao.com/webtranslate'  # 获取翻译加密结果

headers = {
    "Accept": "application/json, text/plain, */*",
    "Accept-Language": "zh-CN,zh;q=0.9",
    "Cache-Control": "no-cache",
    "Connection": "keep-alive",
    "Content-Type": "application/x-www-form-urlencoded",
    "Origin": "https://fanyi.youdao.com",
    "Pragma": "no-cache",
    "Referer": "https://fanyi.youdao.com/",
    "Sec-Fetch-Dest": "empty",
    "Sec-Fetch-Mode": "cors",
    "Sec-Fetch-Site": "same-site",
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.6261.95 Safari/537.36",
    "sec-ch-ua-mobile": "?0"
}

cookies = {
    "OUTFOX_SEARCH_USER_ID": "1655989668@2408:8421:ad52:8fe7:7964:5d21:8d72:6483",
    "OUTFOX_SEARCH_USER_ID_NCOO": "1053287504.71911",
    "DICT_DOCTRANS_SESSION_ID": "NjgxNmI3MTEtZGUxOC00MWZhLWE2OWMtMmFmYjdjZTQ3Y2Y1",
    "_uetsid": "2b566830e92d11ef8d31f3a71f38f335",
    "_uetvid": "2b568c40e92d11efb1e61506e860acd6"
}


def get_sign(time, e):
    with open('youdao_fanyi.js', 'r', encoding='utf-8') as f:  # 使用 utf-8-sig 避免 BOM 问题
        js_content = f.read()
    ctx = execjs.compile(js_content)
    return ctx.call('sign', time, e)


def get_kv():
    import time
    timestamp = str(int(time.time() * 1000))
    params = {
        "keyid": "webfanyi-key-getter",
        "sign": F"{get_sign(timestamp, 'asdjnjfenknafdfsdfsd')}",
        "client": "fanyideskweb",
        "product": "webfanyi",
        "appVersion": "1.0.0",
        "vendor": "web",
        "pointParam": "client,mysticTime,product",
        "mysticTime": F"{timestamp}",
        "keyfrom": "fanyi.web",
        "mid": "1",
        "screen": "1",
        "model": "1",
        "network": "wifi",
        "abtest": "0",
        "yduuid": "abcdefg"
    }
    # 发送GET请求
    response = requests.get(url, headers=headers, params=params)
    res = response.json()
    # secretKey = res['data']['secretKey']
    aesKey = res['data']['aesKey']
    aesIv = res['data']['aesIv']
    return aesKey, aesIv


def get_result():
    import time
    timestamp = str(int(time.time() * 1000))
    data = {
        'i': '你好',
        'from': 'auto',
        'to': '',
        'domain': '0',
        'dictResult': 'true',
        'keyid': 'webfanyi',
        "sign": F"{get_sign(timestamp, 'Vy4EQ1uwPkUoqvcP1nIu6WiAjxFeA3Y9')}",
        'client': 'fanyideskweb',
        'product': 'webfanyi',
        'appVersion': '1.0.0',
        'vendor': 'web',
        'pointParam': 'client,mysticTime,product',
        'mysticTime': timestamp,
        'keyfrom': 'fanyi.web',
        'mid': '1',
        'screen': '1',
        'model': '1',
        'network': 'wifi',
        'abtest': '0',
        'yduuid': 'abcdefg',
    }
    response = requests.post('https://dict.youdao.com/webtranslate', headers=headers, data=data).text
    return response


def decrypt_data(a, b, c):
    with open('youdao_fanyi.js', 'r', encoding='utf-8') as f:  # 使用 utf-8-sig 避免 BOM 问题
        js_content = f.read()
    ctx = execjs.compile(js_content)
    return ctx.call('decodeData', a, b, c)


if __name__ == '__main__':
    t, a = get_kv()
    print(t)
    print(a)
    c = get_result()
    print(c)
    print(decrypt_data(c, t, a))
